/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "world_packet_dump.h"
#include <string>
#include <sstream>
#include "node/util/string.h"
using namespace ::node::util;

void DumpWorldPacket(int conn_id, ::node::log::Log *logger, const WorldPacket *packet)
{
    ::std::string packet_str("");
    const ::std::uint8_t *byte_ptr = packet->contents();
    ::std::size_t p = 0;
    while (true)
    {
        for (size_t j = 0; j < 16 && p < packet->size(); ++j) {
            char buf[0x10] = { 0 };
            string::snprintf(buf, sizeof(buf), "%.2X ", byte_ptr[p++]);
            packet_str.append(buf);
        }

        if (p < packet->size()) {
            packet_str.append("\n");
            continue;
        }
        break;
    }
    logger->debug("[DumpWorldPacket], %d\n"
        "cmd:0x%x\n"
        "size: %d\n"
        "===============================================\n"
        "%s"
        "\n===============================================",
        conn_id, packet->get_opcode(), packet->size(), packet_str.c_str());
}